

const encodeTable = [
  '+', '-', '×', '÷', '=', '≠', '≈', '≡',
  '<', '>', '≤', '≥', '∞', '√', '∛', '∜',
  '∑', '∏', '∫', '∬', '∭', '∮', '∯', '∰',
  '1', '∂', 'Δ', '∇', '∴', '∵', '∠', '∟',
  '∩', '∪', '⊂', '⊃', '⊆', '⊇', '∈', '∉',
  '∅', '∃', '∀', '∧', '∨', '⊕', '⊗', '⊥',
  '∝', '∷', '∸', '∼', '∽', '≃', '≅', '2',
  '∸', '⊢', '⊨', '⊤', '3', '⊼', '⊻', '⊂'
];

/*const encodeTable = [
  '??', '??', '??', '??', '??', '??', '??', '??', 
  '??', '??', '??', '??', '??', '??', '??', '??',
  '??', '??', '??', '??', '??', '??', '??', '??',
  '☺️', '??', '??‍↕️', '??', '??', '??', '??', '??',
  '??', '??', '??', '??', '??', '??', '??', '??',
  '??', '??', '??‍??️', '??', '??', '??', '??', '??',
  '??', '??', '??', '??', '??', '??', '??', '??',
  '??', '??', '??‍??', '??', '??', '??', '??', '??'
];"/"


/*const encodeTable = [
  '┗', '⁠(', '⁠•', '⁠ˇ', '⁠_',
  '⁠―', '⁠→', '⁠☞', 'ಠ','∀',
  '⁠д', '⁠⊰', '⁠.', '๑', '◡',
  'ԅ', '⁠ᕤ', '↼', '｡', '⁠ω',
  '⁠･', '⁠>', '▽', '⁠<', 'ﾉ',
  '⁠*', '⁠꒪', '⁠ヮ', '⁠o', '^',
  '╭', '⁠•́', '⍛', '〈', '⁠ˇ',
  '⁠-', '⁠?', 'σ', '☉', '!',
  '⁠0', 'q', '´', 'ミ', '⁠`',
  '⁠՞', 'ਊ', '❛', '⁠●', '╰',
  'ᗜ', '✧', 'Д', '⁠◕', '⁠➜',
  '⁠￣', '︶', '↗', '⁠･', '↝',
  'ヘ', '⁠□', '◎', '⁠╬'
];*/

/*const encodeTable = [
  '!', '\"', '#', '$', '%', 
  '&', '\'', '(', ')', '*',
  '+', ',', '-', '.', '/', 
  ':', ';', '<', '=', '>',
  '?', '@', '[', ']', '^', 
  '_', '`', '{', '|', '}',
  '~', ' ', '\t', '\n', '\r', 
  '\f', '\v', '\\', '§', '©', 
  '®', '™', 'µ', '¶', '×', 
  '÷', '¥', 'i', '£', '€', 
  '¢', '°', '¬', '±', '·', 
  '¿', '¡', '¤', '¦', '«', 
  '»', '•', '√', '´'
];*/


const binaryMapping = [
  '000000', '000001', '000010', 
  '000011', '000100', '000101', 
  '000110', '000111', '001000', 
  '001001', '001010', '001011', 
  '001100', '001101', '001110', 
  '001111', '010000', '010001', 
  '010010', '010011', '010100', 
  '010101', '010110', '010111',
  '011000', '011001', '011010', 
  '011011', '011100', '011101', 
  '011110', '011111', '100000', 
  '100001', '100010', '100011', 
  '100100', '100101', '100110', 
  '100111', '101000', '101001', 
  '101010', '101011', '101100', 
  '101101', '101110', '101111',
  '110000', '110001', '110010', 
  '110011', '110100', '110101', 
  '110110', '110111', '111000', 
  '111001', '111010', '111011', 
  '111100', '111101', '111110', 
  '111111'
];



function toBinaryString(str) {
  return str.split('')
    .map(c => c.charCodeAt(0).toString(2).padStart(8, '0'))
    .join('');
}

function fromBinaryString(binStr) {
    // Convert binary string to text
    const bytes = binStr.match(/.{8}/g) || [];
    return bytes.map(byte => String.fromCharCode(parseInt(byte, 2))).join('');
}

function en53(input) {
const binaryString = toBinaryString(input);
const blockSize = 30; // Block size in bits (6 segments of 5 bits each)
// Pad binary string to a multiple of blockSize
const paddingLength = (blockSize - (binaryString.length % blockSize)) % blockSize;
const paddedBinaryString = binaryString + '0'.repeat(paddingLength);

//console.log('Block size:', blockSize);
//console.log('Padding Length:', paddingLength);
//console.log('Padded String:', paddedBinaryString);
// Block size: 30
// Padding Length: 16
// Padded String: 010010000110010101101100011011000110111100101100001000000101011101101111011100100110110001100100001000010000000000000000
// Original Input: Hello, World!
// Encoded Input: 01001000011001010110110001101100011011110010110000100000010101110110111101110010011011000110010000100001

let encodedString = '';
for (let i = 0; i < paddedBinaryString.length; i += blockSize) {
  const block = paddedBinaryString.substr(i, blockSize);
     for (let j = 0; j < blockSize; j += 6) {
        const segment = block.substr(j, 6);
        const index = parseInt(segment, 2);
        encodedString += encodeTable[index];
     }
}
  
//return binaryString;
return encodedString;
}

//function de53(binaryString) {
function de53(encoded) {
//return fromBinaryString(binaryString);

let binaryString = '';
for (const char of encoded) {
  //console.log('Encoded...', encoded);
  //console.log('Character...', char);
  const index = encodeTable.indexOf(char);
    if (index === -1) throw new Error('Invalid character in encoded string');
     binaryString += binaryMapping[index];
 }

  const byteString = binaryString.match(/.{8}/g) || [];
  return fromBinaryString(byteString.join(''));
}

// Example usage:
const inputString = "Hello, World!";
//const inputString = "-----Key-----\n"
// +"ABCDEFGHIJKLM\n"
// +"NOPQRSTUVWXYZ\n"
// +"-----End-----\n";
const encodeString = en53(inputString);
const decodeString = de53(encodeString);

console.log('Original Input:\n', inputString);
console.log('Encoded Input:\n', encodeString);
console.log('Decoded Input:\n', decodeString);
// Example implementation of en53
//function en53(input) {
  // Your encoding logic here
  // Placeholder for the actual implementation
  //return input; // Replace with actual encoded string
//}






